package com.wenhuitec.syslib.config;

import android.content.Context;
import android.content.SharedPreferences;

import com.wenhuitec.syslib.utils.SysLoginUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SysLoginConfig {

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SysLoginConfig sysLoginConfig;

    public static SysLoginConfig getInstance(Context context) {
        if (sysLoginConfig == null) {
            sysLoginConfig = new SysLoginConfig(context);
        }
        return sysLoginConfig;
    }

    private SharedPreferences sharedPreferences;

    private SharedPreferences.Editor editor;

    public SysLoginConfig(Context context) {
        sharedPreferences = context.getSharedPreferences("systemI", Context.MODE_PRIVATE); //私有数据
        editor = sharedPreferences.edit();
        userName = sharedPreferences.getString("userName", "");
        password = sharedPreferences.getString("password", "");
        token = sharedPreferences.getString("token", "");
        status = sharedPreferences.getBoolean("status", false);
        try {
            String loginTimeStr = sharedPreferences.getString("loginTime", "2024-01-24 00:00:00");
            loginTime = simpleDateFormat.parse(loginTimeStr);
            String lastTimeStr = sharedPreferences.getString("lastTime", "2024-01-24 00:00:00");
            lastTime = simpleDateFormat.parse(lastTimeStr);
        } catch (Exception e) {
            loginTime = new Date();
            lastTime = new Date();
        }
        SysLoginUtils.setToken(token);

    }

    private String userName;
    private String password;
    private Date loginTime;
    private boolean status;
    private Date lastTime;

    private String token;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
        editor.putString("userName", userName);
        editor.apply();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
        editor.putString("password", password);
        editor.apply();
    }

    public Date getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Date loginTime) {
        this.loginTime = loginTime;
        editor.putString("loginTime", simpleDateFormat.format(loginTime));
        editor.apply();
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
        editor.putBoolean("status", status);
        editor.apply();
    }

    public Date getLastTime() {
        return lastTime;
    }

    public void setLastTime(Date lastTime) {
        this.lastTime = lastTime;
        editor.putString("lastTime", simpleDateFormat.format(lastTime));
        editor.apply();
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
        SysLoginUtils.setToken(token);
        editor.putString("token", token);
        editor.apply();
    }
}
